/*package com.glgc.projecti.web.controller;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.glgc.projecti.common.CommonConstant;
import com.glgc.projecti.common.CommonUtils;
import com.glgc.projecti.common.TripleDESUtils;
import com.glgc.projecti.common.WeixinUtils;
import com.glgc.projecti.entity.OpenUser;
import com.glgc.projecti.entity.dto.ClientLoginer;
import com.glgc.projecti.service.IHttpService;
import com.glgc.projecti.service.IOpenUserService;

@RequestMapping("/race")
@Controller
public class RaceNavigateController {

	@Resource IHttpService httpService;
	
	@Resource
	private IOpenUserService openUserService;
	
	//分享出去的页面
	@RequestMapping("/wx_share.do")
	public String wx_share(HttpSession session, HttpServletRequest request, Model model) throws IOException {
		Map<String, String> wish = WeixinUtils.wish(request);
		model.addAttribute("wish", wish);
		
		openIdAutoLog(session);
		
		return "wx_share"; 
	}
	
	//抽奖页面
	@RequestMapping("/wheel.do")
	public String wheel(HttpSession session, HttpServletRequest request, Model model) throws IOException {
		Map<String, String> wish = WeixinUtils.wish(request);
		model.addAttribute("wish", wish);
		
		openIdAutoLog(session);
		
		ClientLoginer loginer = (ClientLoginer) session.getAttribute(CommonConstant.CLIENT_LOGIN_NAME);
		String openId = (String) session.getAttribute(CommonConstant.WEIXING_OPEN_ID);
		if (loginer != null && openId != null && openUserService.findByOpenId(openId) == null) {
			// 如果登录，抽奖前记录用户信息
			OpenUser openUser = new OpenUser();
			openUser.setOpenId(openId);
			openUser.setUserName(loginer.getUserName());
			openUser.setMobile(loginer.getMobileTel());
			openUserService.saveOrUpdate(openUser);
		}
		return "wheel"; 
	}
	
	//排行榜
	@RequestMapping("/rank.do")
	public String rank(String sourceType, String sourceId, 
			RedirectAttributes attr,HttpSession session, HttpServletRequest request, Model model) throws IOException {
		Map<String, String> wish = WeixinUtils.wish(request);
		if (!CommonUtils.isEmpty(sourceType)) {
			session.setAttribute("sourceType", sourceType);
		}
		model.addAttribute("wish", wish);
		return "rank"; 
	}
	
	
	
	//登陆页面
	@RequestMapping("/login.do")
	public String login(String fundAccount, String mobileTel, String sourceType, String sourceId, 
			RedirectAttributes attr, HttpSession session,Model model, HttpServletRequest request) throws Exception {
		Map<String, String> wish = WeixinUtils.wish(request);
		model.addAttribute("wish", wish);
		if (!CommonUtils.isEmpty(sourceType)) {
			session.setAttribute("sourceType", sourceType);
		}
		
		openIdAutoLog(session);
		
		if (session.getAttribute(CommonConstant.CLIENT_LOGIN_NAME) == null) {
			if(CommonUtils.isEmpty(fundAccount)||CommonUtils.isEmpty(mobileTel)){
				return "login"; 
			}
			ClientLoginer loginer = cacheLogin(fundAccount, mobileTel);
			if (loginer != null) {
				session.setAttribute(CommonConstant.CLIENT_LOGIN_NAME, loginer);
				return "personal";
			}else{
				model.addAttribute("deleteStore", 1);
				return "login"; 
			}
			
		} else {
			return "personal";
		}

	}
	
	
	//无资金帐号登陆页面
	@RequestMapping("/loginWithoutFundAccount.do")
	public String loginWithoutFundAccount(String fundAccount, String mobileTel, String sourceType, String sourceId, 
			RedirectAttributes attr, HttpSession session,Model model, HttpServletRequest request) throws Exception {
		session.setAttribute("sourceType","2");
		
		if (session.getAttribute(CommonConstant.CLIENT_LOGIN_NAME) == null) {
			if(CommonUtils.isEmpty(fundAccount)||CommonUtils.isEmpty(mobileTel)){
				return "login_without_fund_account"; 
			}
			ClientLoginer loginer = cacheLogin(fundAccount, mobileTel);
			if (loginer != null) {
				session.setAttribute(CommonConstant.CLIENT_LOGIN_NAME, loginer);
				return "personal";
			}else{
				model.addAttribute("deleteStore", 1);
				return "login_without_fund_account";
			}
			
		} else {
			return "personal";
		}

	}
	
	//无资金帐号登陆页面
	@RequestMapping("/registerWithoutFundAccount.do")
	public String registerWithoutFundAccount(String fundAccount, String mobileTel, String sourceType, String sourceId, 
			RedirectAttributes attr, HttpSession session,Model model, HttpServletRequest request) throws Exception {
		session.setAttribute("sourceType", "2");
		
		if (session.getAttribute(CommonConstant.CLIENT_LOGIN_NAME) == null) {
			
			if(CommonUtils.isEmpty(fundAccount)||CommonUtils.isEmpty(mobileTel)){
				return "register_without_fund_account"; 
			}
			
			ClientLoginer loginer = cacheLogin(fundAccount, mobileTel);
			if (loginer != null) {
				session.setAttribute(CommonConstant.CLIENT_LOGIN_NAME, loginer);
				return "personal";
			}
			return "register_without_fund_account";
		} else {
			return "personal";
		}
	}
	
	//大赛主页 大赛规则
	@RequestMapping("/racehome.do")
	public String racehome(String sourceType, 
			RedirectAttributes attr,HttpSession session, Model model, HttpServletRequest request) throws IOException {
		if (!CommonUtils.isEmpty(sourceType)) {
			session.setAttribute("sourceType", sourceType);
		}
		Map<String, String> wish = WeixinUtils.wish(request);
		model.addAttribute("wish", wish);
		return "racehome";
	}
	
	*//** 预约报名页面
	 * @return
	 * @throws IOException
	 *//*
	@RequestMapping("/booking.do")
	public String booking() throws IOException {
		return "booking";
	}
	
	// 报名 注册
	@RequestMapping("/register.do")
	public String register(String fundAccount, String mobileTel, String sourceType, String sourceId,
			RedirectAttributes attr,HttpSession session, Model model, HttpServletRequest request) throws Exception {
		Map<String, String> wish = WeixinUtils.wish(request);
		model.addAttribute("wish", wish);
		String redirectUrl = "register";
		if (!CommonUtils.isEmpty(sourceType)) {
			session.setAttribute("sourceType", sourceType);
		}
		openIdAutoLog(session);
		
		if (session.getAttribute(CommonConstant.CLIENT_LOGIN_NAME) == null) {
			
			if(CommonUtils.isEmpty(fundAccount)||CommonUtils.isEmpty(mobileTel)){
				return redirectUrl; 
			}
			
			ClientLoginer loginer = cacheLogin(fundAccount, mobileTel);
			if (loginer != null) {
				session.setAttribute(CommonConstant.CLIENT_LOGIN_NAME, loginer);
				return "personal";
			}
			return redirectUrl;
		} else {
			return "personal";
		}

	}
	
	@RequestMapping("/agree.do")
	public String pn51() throws IOException {
		return "agree";
	}
	
	@RequestMapping("/eventBroadcast.do")
	public String eventBroadcast(Model model, HttpServletRequest request) throws IOException {
		Map<String, String> wish = WeixinUtils.wish(request);
		model.addAttribute("wish", wish);

		return "event_broadcast";
	}
	
	@RequestMapping("/eventBroadcastDetail.do")
	public String eventBroadcastDetail(Model model, HttpServletRequest request) throws IOException {
		Map<String, String> wish = WeixinUtils.wish(request);
		model.addAttribute("wish", wish);

		return "event_broadcast_detail";
	}
	
	@RequestMapping("/pieChart.do")
	public String pieChart(Model model, HttpServletRequest request) throws IOException {
		Map<String, String> wish = WeixinUtils.wish(request);
		model.addAttribute("wish", wish);

		return "pie_chart";
	}
	
	@RequestMapping("/consultantList.do")
	public String consultantList(Model model, HttpServletRequest request) throws IOException {
		Map<String, String> wish = WeixinUtils.wish(request);
		model.addAttribute("wish", wish);

		return "consultant_list";
	}
	
	@RequestMapping("/consultantDetail.do")
	public String consultantDetail(int id, Model model, HttpServletRequest request) throws IOException {
		Map<String, String> wish = WeixinUtils.wish(request);
		model.addAttribute("wish", wish);

		return "consultant_detail";
	}
	
	@RequestMapping("/myQuestion.do")
	public String myQuestion(Model model, HttpServletRequest request) throws IOException {
		Map<String, String> wish = WeixinUtils.wish(request);
		model.addAttribute("wish", wish);

		return "my_question";
	}
	
	@RequestMapping("/6-ggk.do")
	public String pn6ggk() throws IOException {
		return "redirect:/static/6-ggk.html";
	}
	
	private ClientLoginer cacheLogin(String fundAccount, String mobileTel) throws Exception {
		ClientLoginer loginer = new ClientLoginer();
		loginer.setMobileTel(TripleDESUtils.decode(mobileTel));
		loginer.setFundAccount(TripleDESUtils.decode(fundAccount));
		JSONObject result = httpService.login(loginer);
		if (result.get("errorno").toString().equals("0")) {
			
			JSONObject tempJSON = getDataset(result);
			ClientLoginer login = new ClientLoginer();
			login.setFundAccount(loginer.getFundAccount());
			login.setMobileTel(tempJSON.get("mobile_tel").toString());
			login.setUserName(tempJSON.get("user_name").toString());
			login.setContestantId(tempJSON.get("contestant_id").toString());
			return login;
		}
		return null;
	}
	
	private JSONObject getDataset(JSONObject result) {
		try {
			JSONObject tempJSON = result.getJSONObject("result").getJSONArray("dataset0").getJSONObject(0);
			return tempJSON;
		} catch (Exception e) {
			return null;
		}
	}
	
	//根据openid 自动登陆，并设置sourceType 、 CLIENT_LOGIN_NAME
	private void openIdAutoLog(HttpSession session) {
		
		String openId = (String) session.getAttribute(CommonConstant.WEIXING_OPEN_ID);
		if (!CommonUtils.isNotEmpty(openId)) {				// 如果存在openId, 则认为是微信方式
			return ;
		}else{
			session.setAttribute("sourceType", "1");
		}
		
		if(session.getAttribute(CommonConstant.CLIENT_LOGIN_NAME) != null){
			return ;
		}
		ClientLoginer loginer = new ClientLoginer();
		loginer.setSourceType("1");
		loginer.setSourceId(openId);
		JSONObject result = httpService.login(loginer);
		if (result.get("errorno").toString().equals("0")) {
			
			JSONObject tempJSON = getDataset(result);
			
			ClientLoginer login = new ClientLoginer();
			login.setFundAccount(tempJSON.get("fund_account").toString());
			login.setMobileTel(tempJSON.get("mobile_tel").toString());
			login.setUserName(tempJSON.get("user_name").toString());
			login.setContestantId(tempJSON.get("contestant_id").toString());
			login.setSourceFlag("1");
			login.setWechatId(openId);
			session.setAttribute(CommonConstant.CLIENT_LOGIN_NAME, login);
		 
		}
		
	}
	
	
}

*/